package com.team.controller;

import com.team.pojo.AppVersion;
import com.team.service.AppVersionService;
import com.team.util.FileUploadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class AppVersionController {
 @Autowired(required = false)
 private AppVersionService appVersionService;
 @RequestMapping("/goAddVersion")
 public String goAddVersion(Long id, Model model){
  List<AppVersion> list=appVersionService.getVersionByAppId(id);

  //将appid放到页面中，以便后后期添加使用
  model.addAttribute("appVersions",list);
  model.addAttribute("appid",id);
  //返回视图
  return "developer/appversionadd";
 }
 @RequestMapping("/addAppVersion")
 //app版本信息，文件上传，
 public String addAppVersion(AppVersion appVersion,@RequestParam(value = "a_downloadLink",required = false
 ) CommonsMultipartFile file, HttpServletRequest request, HttpSession session){
  try {
   String path=request.getSession().getServletContext().getRealPath("upload/apk");
   String filename=FileUploadUtil.uploadFile(path,file);
   appVersion.setApkfilename(file.getOriginalFilename());
   appVersion.setDownloadlink("upload/apk/"+filename);
   this.appVersionService.addAppVersion(appVersion);
   return "redirect:goAddVersion?id="+appVersion.getAppid();
  } catch (Exception e) {
   e.printStackTrace();
  }
  return "redirect:/jsp/error.jsp";
 }
}
